#!/usr/bin/env python3
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from sqlalchemy import text
from app.db.database import engine

def update_product_images():
    try:
        with engine.connect() as conn:
            print("更新 product_images 表结构...")
            
            # 删除旧表（如果存在）
            conn.execute(text("DROP TABLE IF EXISTS product_images"))
            
            # 创建新表
            conn.execute(text("""
                CREATE TABLE product_images (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    product_id INT,
                    filename VARCHAR(255) NOT NULL,
                    original_filename VARCHAR(255) NOT NULL,
                    file_path VARCHAR(500) NOT NULL,
                    file_size INT NOT NULL,
                    content_type VARCHAR(100) NOT NULL,
                    sort_order INT DEFAULT 0,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL
                )
            """))
            
            conn.commit()
            print("✅ product_images 表更新成功！")
            
    except Exception as e:
        print(f"❌ 更新 product_images 表时出错: {e}")
        return False
    return True

if __name__ == "__main__":
    print("开始更新 product_images 表...")
    if update_product_images():
        print("✅ 数据库更新成功！")
    else:
        print("❌ 数据库更新失败！") 